package com.csq.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.csq.common.pojo.ReturnResult;
import com.csq.common.pojo.StatusCode;
import com.csq.common.util.JsonUtils;
import com.csq.manager.mapper.EnterpriseMapper;
import com.csq.manager.pojo.Enterprise;
import com.csq.manager.pojo.EnterpriseExample;
import com.csq.manager.pojo.EnterpriseExample.Criteria;
import com.csq.manager.service.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private EnterpriseMapper enterpriseMapper;

	@Autowired
	private PoolJedisClient poolJedisClient;

	@Value("${SESSON_KEY}")
	private String SESSON_KEY;
	
	@Value("${SESSON_EXPIE}")
	private int SESSON_EXPIE;

	@Override
	public Enterprise getEnterpriseByID(int enterpriseId) {
		// TODO Auto-generated method stub

		Enterprise selectByPrimaryKey = enterpriseMapper
				.selectByPrimaryKey(enterpriseId);

		return selectByPrimaryKey;
	}

	@Override
	public List<Enterprise> getAllEnterpriseList(Integer typeId) {
		EnterpriseExample enterpriseExample = new EnterpriseExample();
		if (typeId != 2) {
			Criteria createCriteria = enterpriseExample.createCriteria();
			createCriteria.andStateEqualTo(typeId.shortValue());
		}
		return enterpriseMapper.selectByExample(enterpriseExample);
	}

	@Override
	public ReturnResult updateEnterprise(Enterprise enterprise) {

		int updateByPrimaryKeySelective = enterpriseMapper.updateByPrimaryKey(enterprise);
		
		if(updateByPrimaryKeySelective == 0){
			return ReturnResult.build(StatusCode.UPDATEFAIL, "更新失败！！");
		}
		
		enterprise = enterpriseMapper.selectByPrimaryKey(enterprise.getEnterpriseId());
		
		String token = enterprise.getWorkEmail();
		
		// 更新 redis 缓存
		poolJedisClient.set(SESSON_KEY + ":" + token,
				JsonUtils.objectToJson(enterprise));

		poolJedisClient.expire(SESSON_KEY + ":" + token, SESSON_EXPIE);
		
		return ReturnResult.ok();
	}

}
